var topNodeABO = 0;
var currentNode;
var currentNodeABO = 0;
var currentNodeName;
var parentNodeABO = 0;
var totalNumber = 0;

var doneLosChartShow = false;
var waitForLosChartShow = false;

function getPINLevel(point) {
	if (point < 200000)
		return '0%';
	else if (point < 600000)
		return '3%';
	else if (point < 1200000)
		return '6%';
	else if (point < 2400000)
		return '9%';
	else if (point < 4000000)
		return '12%';
	else if (point < 6800000)
		return '15%';
	else if (point < 10000000)
		return '18%';
	else if (point < 20000000)
		return '21%';
	else
		return 'RUBY';
	
	return 'undefined';
}

function expandAndShowAll()
{
	$('#LOSChart').hide();
	doneLosChartShow = false;
	currentNode = $('#treeList');
	showLOSChartAndExpand();
}

function getIDAndName(anchorObject) {
	//href="javascript:downPlus('7063489');LosChartShow('7063489', '7063489', '怨듭＜&�뺤옄');"
	var StrLosChartShow = 'LosChartShow';
	var href = $(anchorObject).attr('href');
	var index = href.indexOf(StrLosChartShow);
	//console.log('[JS][getIDAndName] index : ' + index + ', href : ' + href);
	
	if (index >= 0) {
		index += StrLosChartShow.length;
		
		var params = href.substring(index);
		params = params.replace(/[(]/g, '');
		params = params.replace(/[)]/g, '');
		params = params.replace(/[']/g, '');
		params = params.replace(/[ ]/g, '');
		params = params.replace(/[;]/g, '');
		
		var values = params.split(',');
		// id
		currentNodeABO = values[0];
		currentNodeName = values[2];
		console.log('[JS] getIDAndName : ' + currentNodeABO + ', ' + currentNodeName);
		return values;
	}
	return null;
}

function fakeClick(liTag)
{
	var anchorObject = $(liTag).find('a')[0];
	var clickEvent = new MouseEvent('click');
	anchorObject.dispatchEvent(clickEvent);
	
	var values = getIDAndName(anchorObject);
	// id
	currentNodeABO = values[0];
	currentNodeName = values[2];
	console.log('[JS] currentNode : ' + currentNodeABO + ', ' + currentNodeName);
	
	var id = $(liTag).attr('id');
	if (id != null) {
		console.log('[JS] liTag : id=' + id);
	}
	
	var parentLiTag = $(liTag).closest('li');
	if (parentLiTag != null) {
		id = $(parentLiTag).attr('id');
		if (id != null) {
			console.log('[JS] parentLiTag : id=' + id);
		}
		anchorObject = $(parentLiTag).find('a')[0];
		values = getIDAndName(anchorObject);
		parentNodeABO = values[0];
	}
	else {
		parentNodeABO = currentNodeABO;
	}
	
	if (totalNumber == 1) {
		topNodeABO = currentNodeABO;
		
	}
}

function showLOSChartAndExpand()
{
	if ($('#LOSChart').css('display') == 'block'
		&& $('#chart02').text() != ''
		&& !$(currentNode).hasClass('treePlus'))
	{
		waitForLosChartShow = false;
		setInformation();
	}
	
	if (waitForLosChartShow == false)
	{
		totalNumber++;
		var liTag = findNextNode(currentNode);
		if (liTag == null) {
			setTotalABO();
			$('#LOSChart').hide();
			alert('완료');
			return;
		}

		// init pv value
		$('#LOSChart').hide();
		
		fakeClick(liTag);
		
		waitForLosChartShow = true;
		currentNode = liTag;
	}

	setTimeout(function() {
		showLOSChartAndExpand();
	}, 100);
}

function setTotalABO()
{
	var html = $('#pv_' + topNodeABO).html();
  $('#pv_' + topNodeABO).html(html + '&nbsp;&nbsp;&nbsp;&nbsp;회원수:' + totalNumber);	
	$("#pv_" + topNodeABO).css("color", "#C26900");
}

function setInformation()
{
	var abo = currentNodeABO;
	var pv = $('#chart02').text();
	var pvNum = Number(pv.trim().replace(/[^0-9]/g, ''));
	var pin = getPINLevel(pvNum);
	$('#pv_' + abo).html('&nbsp;&nbsp;&nbsp;&nbsp;' + pin + '&nbsp;&nbsp;&nbsp;&nbsp;PV : ' + pv);	
	$("#pv_" + abo).css("color", "#C26900");
	//console.log('[JS][setInformation] abo : ' + abo + ', PIN : ' + pin + ', PV : ' + pv);
		console.log('[JS][setInformation] window.JSInterface.addMember(' + abo + ', ' + currentNodeName + ', ' + pin + ', ' + Number(parentNodeABO) + ');');
		console.log('[JS][setInformation] window.JSInterface.addStatsRecord(' + abo + ', ' + pvNum + ', 0);');
}

function findNextNode(node)
{
	// node媛 ul �� 寃쎌슦 泥ル쾲吏� li瑜� 由ы꽩
	var firstLi = $(node).children('li:first')[0];
	if (firstLi) {
		return firstLi;
	}

	// node媛 li �� 寃쎌슦 ul �먯떇 以묒뿉 泥ル쾲吏� li瑜� 由ы꽩
	var ulNode = $(node).children('ul')[0];
	if (ulNode) {
		firstLi = $(ulNode).children('li:first')[0];
		if (firstLi) {
			return firstLi;
		}
	}
	
	// �녿떎硫� next sibling 李얠븘�� 由ы꽩
	var nextSibling = $(node).next();
	if (nextSibling != null && $(nextSibling).is('li')) {
		return nextSibling;
	}
	
	var parent = node;
	while (true) {
		// �녿떎硫� 遺紐⑥쓽 �ㅼ쓬 Li瑜� 李얠븘�� 由ы꽩
		var parent = $(parent).closest('ul');
		if ($(parent).is('#treeList')) {
			console.log('[JS][findNextNode] finish at treeList');
			return null;
		}
		
		parent = $(parent).closest('li')
		var nextLiNodeOfParentLi = $(parent).next();
		if (nextLiNodeOfParentLi != null && $(nextLiNodeOfParentLi).is('li')) {
			return nextLiNodeOfParentLi;
		}
	}
	
	console.log('[JS][findNextNode] finish, no more');
	return null;
}

expandAndShowAll();
